package com.wx.core.process.message.event;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.wx.core.process.BaseProcess;
import com.wx.core.process.BaseResult;
import com.wx.core.process.ProcessResult;
import com.wx.core.vo.MsgRequest;
import com.wx.entity.WxFansUser;
import com.wx.service.WxFansUserService;
import com.zyht.bank.entity.AxdCustInfo;
import com.zyht.bank.entity.AxdCustInfoExample;
import com.zyht.bank.service.AxdCustInfoService;
import com.zyht.common.bean.ThreadPoolWrapperBean;

/**
 * 扫码已关注
 * 
 * @author qjl
 * @data 2017年7月6日
 */
public class ScanEventProcess extends BaseProcess {
	private String AXD_ORG_CODE = "axd_org_";
	@Resource
	private WxFansUserService wxFansUserService;
	@Resource
	private AxdCustInfoService axdCustInfoService;

	@Override
	public BaseResult process(HttpServletRequest req, HttpServletResponse res, MsgRequest msgReq) {

		ProcessResult result = new ProcessResult();
		String qrscene = msgReq.getEventKey();
		String openid = msgReq.getFromUserName();
		WxFansUser wxfan = wxFansUserService.selectByOpenId(openid);
		Runnable run = () -> {
			if (qrscene != null && qrscene.startsWith("qrscene_")) {
				String orgCode = qrscene.replace("qrscene_", "");
				if (orgCode.startsWith(AXD_ORG_CODE)) {
					orgCode=orgCode.replace(AXD_ORG_CODE, "");
					AxdCustInfoExample example = new AxdCustInfoExample();
					example.createCriteria().andFanUserIdEqualTo(wxfan.getId());
					List<AxdCustInfo> list = axdCustInfoService.list(example);
					if (list.size() > 0) {
						AxdCustInfo axdCustInfo = list.get(0);
						if (StringUtils.isEmpty(axdCustInfo.getLoanOrgCode())) {
							axdCustInfo.setLoanOrgCode(orgCode);
						}
						axdCustInfo.setCreateTime(new Date());
						axdCustInfoService.updateById(axdCustInfo);
					}
				}
			}

		};
		ThreadPoolWrapperBean.getThreadPool().putThread(run);
		result.setResult("success");
		return result;
	}
}
